package terminal.utility;

import java.awt.Color;
import java.util.Calendar;
import java.util.Date;
import javax.swing.*;

public class Field extends JPanel {

    public String fieldNum;
    private String value;
    private JLabel labelShow;
    private JLabel valueShow;
    private JSeparator separator;
    private JSpinner conflatBox;
    private Calendar releaseTime;
    private int mode;
    private int cfTime;

    public Field(String fieldNum, String value) {
        this.fieldNum = fieldNum;
        this.value = value;
        mode = 0;
        cfTime = 0;
        setBackground(new Color(255, 255, 230));
        setLayout(null);

        labelShow = new JLabel();
        labelShow.setFont(new java.awt.Font("Tahoma", 0, 12));
        labelShow.setForeground(new java.awt.Color(102, 102, 102));
        labelShow.setText(this.fieldNum + ":");
        labelShow.setBounds(0, 0, 200, 10);
        add(labelShow);

        valueShow = new JLabel();
        valueShow.setFont(new java.awt.Font("Tahoma", 0, 12));
        valueShow.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        valueShow.setText(value);
        valueShow.setBounds(0, 10, 200, 10);
        add(valueShow);

        conflatBox = new javax.swing.JSpinner(new SpinnerNumberModel(0, 0, 100000, 1));
        conflatBox.setVisible(false);
        conflatBox.setBounds(140, 0, 60, 20);
        add(conflatBox);

        separator = new JSeparator();
        separator.setBounds(0, 22, 200, 5);
        add(separator);

    }

    public void newValue(String value) {
        this.value = value;
    }

    public void update() {
        valueShow.setText(value);
    }
    
    public void switchMode() {
        if (mode % 2 == 1) {
            mode -= 1;
            viewMode();
            int cfTime = (Integer) conflatBox.getValue();
            if (cfTime == 0) {
                this.cfTime = 0;
            } else {
                setCFTime(cfTime);
            }
        } else {
            mode += 1;
            configMode();
        }
    }

    public void setCFTime(int sec) {
        cfTime = sec;
        if (cfTime / 60 == 0) {
            releaseTime = Calendar.getInstance();
            releaseTime.set(Calendar.SECOND, (new Date().getSeconds() + cfTime));
        } else if (cfTime / 60 > 0 && cfTime / 3600 < 0) {
            int min = cfTime / 60;
            int seconds = cfTime - min;
            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.MINUTE, new Date().getMinutes() + min);
            cal.set(Calendar.SECOND, new Date().getSeconds() + seconds);
        }
    }

    public void setNewTime() {
        if (cfTime / 60 == 0) {
            releaseTime = Calendar.getInstance();
            releaseTime.set(Calendar.SECOND, (new Date().getSeconds() + cfTime));
        } else if (cfTime / 60 > 0 && cfTime / 3600 < 0) {
            int min = cfTime / 60;
            int seconds = cfTime - min;
            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.MINUTE, new Date().getMinutes() + min);
            cal.set(Calendar.SECOND, new Date().getSeconds() + seconds);
        }
    }

    public String getTime() {
        if (cfTime == 0) {
            return "now";
        } else {
            return releaseTime.getTime().toString();
        }
    }

    public void setCFBox(int num){
        conflatBox.setValue(num);
    }
    
    private void viewMode() {
        valueShow.setVisible(true);
        conflatBox.setVisible(false);
    }

    private void configMode() {
        valueShow.setVisible(false);
        conflatBox.setVisible(true);
    }
}
